package cwb1;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.EntityNotFoundException;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;



public class TrackingServlet extends HttpServlet
{
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String activityType = req.getParameter("activity");
		String userId = req.getParameter("userId");
				
		try {
			User user = UserRegistry.getInstance().get(userId);
			Date currentTime = Calendar.getInstance().getTime();
			
			Activity activity = null;
			if(activityType.equals("Sleep")) {
				activity = new Sleep(currentTime, user.getId());
			}
			else if(activityType.equals("Night life")) {
				activity = new Nightlife(currentTime, user.getId());
			}
			else if(activityType.equals("Job")) {
				activity = new Job(currentTime, user.getId());
			}
			else if(activityType.equals("Sport")) {
				activity = new Sport(currentTime, user.getId());
			}
			else {
				String LearningType = req.getParameter("LearningType");
				String courseId = req.getParameter("course");
				if(LearningType.equals("College")) {
					activity = new College(currentTime, user.getId(), courseId );
				}
				else if(LearningType.equals("Practice")) {
					activity = new Practice(currentTime, user.getId(), courseId);
				}
				else {
					activity = new Study(currentTime, user.getId(), courseId);
				}
			}
			
			ActivityRegistry.getInstance().add(activity);
			user.setCurrentActivityId(activity.getId());
			UserRegistry.getInstance().update(user);
			
			System.out.println(activity.getId());
		}
		
		catch (EntityNotFoundException e) {
			
		}
		
		resp.sendRedirect("timer.jsp");
	}
	
}